<?php
/**
 * utils.php
 *
 * <pre>
 * Devyra Internacionalnaya
 * Vebba PHP Framework and Website Builder
 * File containing miscellaneous functionalities
 * </pre>
 *
 * @package   classes.core.static
 * @version   0.7.0102 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Internacionalnaya
 */
 
/**
 * Class contains miscellaneous utils
 *
 * @package classes.core.static
 * @version 0.7.0102 alpha
 * @since   2012
 * @author  Paul Dinh
 */
class utils {

  /**
   * Default instance level constructor
   */
  public function __construct() {
    //
  }

  /**
   * Standard initialisation
   */
  public static function initialise() {
    //
  }
  
  /**
   * Trim trailing & tailing characters, also extra spaces in between
   *
   * @param  string $Text The text to trim
   * @return string The trimmed text
   */
  public static function trim_neat($Text) {
    $Text = trim($Text);
    while (strpos($Text,chr(32).chr(32))!==false)
      $Text = str_replace(chr(32).chr(32),chr(32),$Text);
    return $Text;
  }
  
  /**
   * Read a .pso file and deserialise into variable
   *
   * @param  string $File_Path The full file path of a .pso file
   * @return mixed  The variable obtained after deserialisation
   */
  public static function var_from_pso($File_Path) {
    $Text = file_get_contents($File_Path);
    return unserialize($Text);
  }
  
  /**
   * Convert object into multi-level array
   *
   * @param  object $Object The object to convert
   * @return array The corresponding multi-level array to the object   
   */
  public static function obj_to_arr($Object) {
    if (!is_object($Object) && !is_array($Object))
      return $Object;
  
    //fun stuff
    if (is_object($Object))
      $Properties = get_object_vars($Object);
    else
      $Properties = $Object;
    
    //convert
    $Array = array();
    foreach ($Properties as $Name=>$Value) 
      if (is_object($Value) || is_array($Value))
        $Array[$Name] = self::obj_to_arr($Value);
      else
        $Array[$Name] = $Value; 
     
    if (count($Array)>0)     
      return $Array;
    else
      return "";
  }//obj_to_arr
  
  /**
   * Get value from a certain path in multi-level array
   *
   * @param  array  $Array    The multi-level array
   * @param  string $Arr_Path The path in array, can not be blank
   * @return object The value of that array level (object or primitive)
   */
  public static function get_arr_value($Array,$Arr_Path) {
    $Arr_Path = str_replace(chr(32),"",$Arr_Path);
    $Tokens = explode("/",$Arr_Path);
    
    //obtain value
    $Value = $Array;
    foreach ($Tokens as $Token) 
      $Value = $Value[$Token];
    
    //return
    return $Value;
  }  
  
  /**
   * Get file extension 
   *
   * @param  string $File_Path The path to a file
   * @return string The file extension in lowercase
   */
  public static function get_file_ext($File_Path) {
    $Parts = pathinfo($File_Path);
    if (array_key_exists("extension",$Parts))
      return strtolower($Parts["extension"]);
    else
      return "";
  }
  
  /**
   * Get file names and dir names in a directory
   *
   * @param  string $Dir_Path The path to directory
   * @return array  The list of files (at least 2, '.' & '..')
   */
  public static function get_files_in($Dir_Path,$Extension=null) {
    if (!file_exists($Dir_Path))
      return array();
    if ($Extension!=null)
      $Extension = strtolower($Extension);
  
    $Dir_Handle = opendir($Dir_Path);
    if ($Dir_Handle===false)
      return array();
      
    $Files = array();
    while (($File=readdir($Dir_Handle))!==false) {
      if ($Extension==null)
        array_push($Files,$File);    
      else
      if (strtolower(self::get_file_ext($File))==$Extension)
        array_push($Files,$File);
    }
      
    closedir($Dir_Handle);
    sort($Files);
    return $Files;
  }
}

//END-OF-FILE
